//
//  CHHub3Device+Gatt.swift
//  SesameSDK
//
//  Created by eddy on 2023/12/27.
//  Copyright © 2023 CandyHouse. All rights reserved.
//

import Foundation

extension CHHub3Device {
    
    func onGattHub3Publish(_ payload: SesameOS3PublishPayload) {
        let itemCode = payload.itemCode
        let data = payload.payload
        L.d("[Hub3][onGattHub3Publish]itemCode:",itemCode.rawValue,"data:", data.bytes)
        switch itemCode {
        case .mechSetting:
            guard data.count > 0 else { break }
            mechSetting?.wifiSSID = String(data: data[0..<30], encoding: .utf8)
            mechSetting?.wifiPassword = String(data: data[30..<60], encoding: .utf8)
            (delegate as? CHWifiModule2Delegate)?.onAPSettingChanged(device: self, settings: mechSetting!)
        case .mechStatus:
            var content = data.copyData
            guard content.count > 0 else { break }
            let isAp: Bool = (content[0...0].toInt8() & 2) > 0
            let isNet: Bool = (content[0...0].toInt8() & 4) > 0
            let isIoT: Bool = (content[0...0].toInt8() & 8) > 0
            let isAPConnecting: Bool = (content[0...0].toInt8() & 32) > 0
            let isNETConnecting: Bool = (content[0...0].toInt8() & 64) > 0
            let isIOTConnecting: Bool = content[0...0].toInt8() < 0
            mechStatus = CHWifiModule2NetworkStatus(isAPWork: isAp, isNetwork: isNet, isIoTWork: isIoT, isBindingAPWork: isAPConnecting, isConnectingNetwork: isNETConnecting, isConnectingIoT: isIOTConnecting)

        case .pubKeySesame:
            var sesame2Keys = [String: String]()
            let dividedData = data.divideArray(chunkSize: 23)
            for (index, keyData) in dividedData.enumerated() {
                let lockStatus = keyData[22]
                // L.d("lockStatus!!!",lockStatus)
                if lockStatus != 0 {
                    let deviceIDData = keyData[0...15]
                    if let sesame2DeviceId = deviceIDData.toHexString().noDashtoUUID() {
                        sesame2Keys[sesame2DeviceId.uuidString] = "\(index)"///hub3 不兼容老设备，这里不消费锁状态，value 用来排序
                    }
                }
            }
            self.sesame2Keys = sesame2Keys
            L.d("sesame2Keys",sesame2Keys)
        case .moveTo:
            if var percentage = data[safeBound: 0...0]?.copyData {
                L.d("[hub3 ] ota ",percentage)
                (delegate as? CHWifiModule2Delegate)?.onOTAProgress(device: self, percent: percentage.toUInt8())
            }
        case .HUB3_ITEM_CODE_SSID_FIRST:break
        case .HUB3_ITEM_CODE_SSID_LAST: break
        case .HUB3_ITEM_CODE_SSID_NOTIFY:
            guard data.count > 1 else { break }
            var rssiData = data[safeBound: 0...1]!.copyData
            let ssidData = data[2...]
            let rssi = rssiData.toInt16()
            if let ssid = String(data: ssidData.copyData, encoding: .utf8) {
                (delegate as? CHWifiModule2Delegate)?.onScanWifiSID(device: self, ssid: CHSSID(name: ssid, rssi: rssi))
            }
        case .SSM_OS3_IR_CODE_FIRST:
            (delegate as? CHHub3Delegate)?.onIRCodeReceiveStart(device: self)
        case .SSM_OS3_IR_CODE_LAST:
            (delegate as? CHHub3Delegate)?.onIRCodeReceiveEnd(device: self)
        case .SSM_OS3_IR_CODE_NOTIFY:
            guard data.count > 0 else { break }
            (delegate as? CHHub3Delegate)?.onIRCodeReceive(device: self, ir: CHHub3IRCode.fromData(data.copyData))
        case .SSM_OS3_IR_CODE_CHANGE:
            (delegate as? CHHub3Delegate)?.onIRCodeChanged(device: self, ir: CHHub3IRCode.fromData(data.copyData))
        case .SSM_OS3_IR_CODE_DELETE:
            (delegate as? CHHub3Delegate)?.onIRCodeChanged(device: self, ir: CHHub3IRCode.fromData(data.copyData))
        case .SSM_OS3_IR_MODE_GET:
            (delegate as? CHHub3Delegate)?.onIRModeReceive(device: self, mode: data.uint8)
        default:
            L.d("!![hub3][pub][\(itemCode.rawValue)]")
        }
    }
}
